package org.czl.gh;

public class l788 {
    public static void main(String[] args) {
        System.out.println(rotatedDigits(10));
    }
    //判断1 - n之中哪些数字只包含一下数字
    // 0 1 8 2 5 6 9
    static int offset;
    public static int rotatedDigits(int n){
        char[] nums = Integer.toString(n).toCharArray();
        int[] pre = new int[]{0, 1, 2, 5, 6, 8, 9};
        offset = nums.length;
        return f(nums, pre, false, true, 0);
    }
    static int f(char[] nums, int[] pre, boolean isLeg, boolean isLimited, int index){
        if(index == offset){
            return isLeg ? 1 : 0;
        }
        int ans = 0;
        int high = isLimited ? nums[index] - '0' : 9;
        for(int i = 0; i < 6; i++){
            int num = pre[i];
            if(num <= high){
                ans += f(nums, pre, num == 2 || num == 5 || num == 6 || num == 9
                        ,isLimited && num == high, index + 1);
            }
        }
        return ans;
    }
}
